﻿/*************************   Auto Generated Repository Code For Encounter table     Generated @ 4/25/2013 9:41:13 PM *************************/


using System;
using System.Collections.Generic;
using System.Linq;
using BusinessObjects;

namespace DataLayer
{
    public partial class EncounterRepository
    {
        private PracticeSimpleDataContext dB;


        /// <summary>
        /// Retrieve an Encounter by its EncounterID (Primary Key)
        /// </summary>
        /// <param name="EncounterID">The ID of the <see cref="Encounter"/> to retrieve</param>
        /// <returns>A <see cref="Encounter"/> object</returns>
        public BusinessObjects.Encounter Select(int EncounterID)
        {
            Encounter retObject;

            using (dB = new PracticeSimpleDataContext())
            {
                retObject = (from x in dB.Encounters where x.EncounterID == EncounterID select x).SingleOrDefault();
            }

            return ConvertToBusinessObject(retObject);
        }

        /// <summary>
        /// Return a list of Encounter objects
        /// </summary>
        /// <returns>A List of type <see cref="Encounter"/></returns>
        public List<BusinessObjects.Encounter> SelectAll()
        {
            List<BusinessObjects.Encounter> retObject = new List<BusinessObjects.Encounter>();

            using (dB = new PracticeSimpleDataContext())
            {
                var EncounterList = (from x in dB.Encounters select x).ToList();

                foreach (Encounter obj in EncounterList)
                {
                    retObject.Add(ConvertToBusinessObject(obj));
                }

            }
            return retObject;
        }


        /// <summary>
        /// Accepts a Encounter Business object, converts it and saves it to the database
        /// </summary>
        /// <returns>Void</returns>
        public void Save(BusinessObjects.Encounter businessObject)
        {
            using (dB = new PracticeSimpleDataContext())
            {
                dB.Encounters.InsertOnSubmit(ConvertToDataObject(businessObject));
                dB.SubmitChanges();
            }
        }

        /// <summary>
        /// Accepts the Primary Key of a Encounter, finds it in the database and deletes it
        /// </summary>
        /// <param name="EncounterID">The ID of the <see cref="Encounter"/> to retrieve</param>
        /// <returns>Void</returns>
        public void Delete(int EncounterID)
        {
            using (dB = new PracticeSimpleDataContext())
            {
                DataLayer.Encounter obj = (from x in dB.Encounters where x.EncounterID == EncounterID select x).SingleOrDefault();
                dB.Encounters.DeleteOnSubmit(obj);
                dB.SubmitChanges();
            }
        }

        /// <summary>
        /// Converts a Encounter data object to a Encounter business object
        /// </summary>
        /// <returns>A business object of type <see cref="Encounter"/></returns>
        public BusinessObjects.Encounter ConvertToBusinessObject(DataLayer.Encounter dataObject)
        {
            BusinessObjects.Encounter businessObject = new BusinessObjects.Encounter();

            businessObject.EncounterID = dataObject.EncounterID;
            businessObject.FacilityID = dataObject.FacilityID;
            businessObject.PatientID = dataObject.PatientID;
            businessObject.EncounterDate = dataObject.EncounterDate;


            return businessObject;

        }

        /// <summary>
        /// Converts a Encounter business object to a Encounter data object
        /// </summary>
        /// <returns>A data object of type <see cref="Encounter"/></returns>
        public DataLayer.Encounter ConvertToDataObject(BusinessObjects.Encounter businessObject)
        {
            DataLayer.Encounter dataObject = new DataLayer.Encounter();

            dataObject.EncounterID = businessObject.EncounterID;
            dataObject.FacilityID = businessObject.FacilityID;
            dataObject.PatientID = businessObject.PatientID;
            dataObject.EncounterDate = businessObject.EncounterDate;


            return dataObject;
        }
    }
}


